// pages/news/news.js
const db = wx.cloud.database()
var times = require('../../utils/times.js')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        book_id: '',
        books_list: [],
        activeName: ['1'],
        show: false,
        show2: false,
        book_Padate: '',
        books: '',
        book_name: '',
        book_writer: '',
        book_introduction: '',
        book_img: ''
    },

    onChange(event) {
        this.setData({
            activeName: event.detail,
        });
    },
    showPopup() {
        this.setData({ show: true });
    },
    onClose() {
        this.setData({ show: false, show2: false });
    },
    Try_read() {
        this.setData({ show2: true })
    },
    //获取书籍数据
    getbookList() {
        db.collection("books_list").doc(this.data.book_id).get().then(res => {
            this.setData({
                books_list: res.data,
                book_Padate: times.toDate(res.data["book_Padate"]),
                book_name: res.data.book_name,
                book_introduction: res.data.book_introduction,
                book_img: res.data.book_img,
                book_writer: res.data.book_writer
            })
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            book_id: options.book_id
        })
        this.getbookList()
    },
    //跳转到借阅书籍页面
    Borrow_books(e) {
        wx.navigateTo({
            url: '../Borrow_books/Borrow_books?Borrow_id=' + e.currentTarget.id,
        })
    },
    //收藏书籍
    Collection(e) {
        let phone = wx.getStorageSync('user')
        if (phone == '') {
            wx.showToast({
                title: '请先登录！',
                icon: 'error'
            })
        } else {
            this.setData({ phone: phone._id })
            db.collection("my_collect").add({
                data: {
                    phone: this.data.phone,
                    collect_name: this.data.book_name,
                    collect_dec: this.data.book_introduction,
                    collect_img: this.data.book_img,
                    collect_writer: this.data.book_writer,
                    _createTime: Date.parse(new Date())
                },
                success: (res => {
                    wx.showToast({
                        title: '收藏成功',
                        icon: 'success'
                    })
                })
            })
        }
    },



    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})